import {userApi} from "../../http/modules/user";

export default {
  state: {
    userInfo: {},
    permissions: [], // 用户权限标识集合
    roles: [],
    dialogFormVisible: false
  },
  mutations: {
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      sessionStorage.setItem("roles", roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      const list = {}
      for (let i = 0; i < permissions.length; i++) {
        list[permissions[i]] = true
      }
      state.permissions = list
      sessionStorage.setItem("permissions", state.permissions)
    }
  },
  actions: {
    GetUserInfo({commit}) {
      return new Promise((resolve, reject) => {
        let username = sessionStorage.getItem('user')
        userApi.getUserInfo({username: username}).then(res => {
          const data = res.data|| {}
          let sysUser = {
            'deptId': data.deptId,
            'deptName': data.deptName,
            'email': data.email,
            'avatar': data.avatar,
            'phone': data.phone,
            'username': data.username,
            'userId': data.userId
          }
          commit('SET_USER_INFO', sysUser)
          commit('SET_ROLES', data.sysRoles || [])
          commit('SET_PERMISSIONS', data.permissions || [])
          resolve(data)
        }).catch((err) => {
          reject()
        })
      })
    }
  }
}
